package com.chen.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.VIP;
import com.chen.dao.mapper.TradeMapper;
import com.chen.dao.mapper.UserMapper;
import com.chen.dao.pojo.Course;
import com.chen.dao.pojo.Trade;
import com.chen.dao.pojo.User;
import com.chen.service.UserService;
import com.chen.vo.Result;
import com.chen.vo.UserVo;
import com.chen.vo.param.UserParam;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:chenao
 * @DATE:2022/3/13 18:09
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TradeMapper tradeMapper;

    private static final String formatPattern="yyyy-MM-dd HH:mm:ss";

    @Override
    public Result listUser(UserParam param) {
        Page<User> page = new Page<>(param.getPage(),param.getLimit());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getId,param.getId());
        //非空判断，防止数据库空值导致查询不到数据，而数据库中的id不可能为空，所以无需判断
        if (Strings.isNotBlank(param.getNickName())) {
            wrapper.like(User::getNickName, param.getNickName());
        }
        if (Strings.isNotBlank(param.getCategory())) {
            wrapper.like(User::getCategory,param.getCategory());
        }
        userMapper.selectPage(page,wrapper);
        List<UserVo> userVoList = new LinkedList<>();
        page.getRecords().stream().forEach(item->{
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(item,userVo);
            userVoList.add(userVo);
        });
        return Result.success(userVoList,page.getTotal());
    }

    @Override
    public Result updateCategory(UserParam param) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,param.getId());
        User user = new User();
        user.setCategory(param.getCategory());
        userMapper.update(user,wrapper);
        return Result.success("ok");
    }

    @Override
    public Result updateBalance(UserParam param) {
        if (param.getNumber()<0||param.getNumber()==null){
            return Result.fail(10000,"输入金额不正确");
        }
        Integer balance = queryBalance(param.getId());
        User user = new User();
        user.setId(param.getId());
        if (balance==null){
            user.setBalance(param.getNumber());
        }else{
            user.setBalance(balance+param.getNumber());
        }
        userMapper.updateById(user);
        Trade trade = new Trade();
        trade.setUserId(param.getId());
        trade.setPayNum(param.getNumber());
        trade.setPayDate(DateUtil.format(new Date(),formatPattern));
        tradeMapper.insert(trade);
        return Result.success("充值成功");
    }

    @Override
    public Integer queryBalance(String userId) {
        User user = userMapper.selectById(userId);
        return user.getBalance();
    }

    @Override
    public Result resetUser(UserParam param) {
        User user = new User();
        user.setId(param.getId());
        user.setBalance(0);
        user.setOverTimes(0);
        user.setDeadline("");
        user.setCategory(VIP.commonVIP);
        userMapper.updateById(user);
        return Result.success("ok");
    }

    @Override
    public Result statisticTrade() {
        List<Trade> tradeList = tradeMapper.selectList(null);
        int count[] = new int[12];
        tradeList.stream().forEach(item->{
            DateTime parse = DateUtil.parse(item.getPayDate(), formatPattern);
            if (parse.year()==DateUtil.year(new Date())){
                count[parse.month()]+=item.getPayNum();
            }
        });
        return Result.success(count);
    }


}
